package com.example.worddemo;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.ImageType;
import com.spire.doc.documents.XHTMLValidationType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * html转图片
 */
public class HTMLtoIMG {
    public static void main(String[] args)throws IOException {
        //创建Document类的实例
        Document document = new Document();

        //加载HTML文档
        document.loadFromFile("table2ToHtml.html", FileFormat.Html, XHTMLValidationType.None);

        //保存为图片格式（支持：BMP, JPEG, PNG, GIF, Tiff等多种图片格式）
//        BufferedImage image = document.saveToImages(0, ImageType.Bitmap);
        //大小适中image
        BufferedImage[] bufferedImages = document.saveToImages(0, 1, ImageType.Bitmap, 200, 300);
        BufferedImage image = bufferedImages[0];
        String result = "HtmlToImage.png";
        File file = new File(result);
        ImageIO.write(image, "PNG", file);
    }
}